function write_gmsh_pos(Node, Elem, val, filename)
% function write_gmsh_pos(Nodes, Elem, val, filename)
% write the nodes' coordinates Nodes and value into .pos file
% val could be vector-valued, stored in column-wisely
%

out = fopen(filename,'w');

% begin of the file
fprintf(out,'View "background mesh" {\n');

N_elem = size(Elem,1);
N_val = size(val, 2); % the length of the value per node

for i = 1:N_elem
    v1 = Elem(i,1);
    v2 = Elem(i,2);
    v3 = Elem(i,3);
    v4 = Elem(i,4);
   
    fprintf(out, 'ST(%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f){', ...
            Node(v1,1), Node(v1,2), Node(v1,3), ...
            Node(v2,1), Node(v2,2), Node(v2,3), ...
            Node(v3,1), Node(v3,2), Node(v3,3), ...
            Node(v4,1), Node(v4,2), Node(v4,3));
        
    for j = 1:N_val-1
        frpintf(out, '%f,', val(i,j));
    end
    fprintf(out, '%f};\n', val(i, N_val));
end


% end of file
fprintf(out, '};');
fclose(out);